package com.wrs.examsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Student;
import com.wrs.examsystem.entity.Teacher;
import com.wrs.examsystem.entity.User;
import com.wrs.examsystem.entity.bean.EnrollBean;
import com.wrs.examsystem.entity.bean.LoginBean;
import com.wrs.examsystem.entity.bean.NewUserBean;
import com.wrs.examsystem.mapper.StudentMapper;
import com.wrs.examsystem.mapper.TeacherMapper;
import com.wrs.examsystem.mapper.UserMapper;
import com.wrs.examsystem.service.StudentService;
import com.wrs.examsystem.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrs.examsystem.util.MD5Util;
import com.wrs.examsystem.util.MyShiro.JWT.JWTUtil;
import com.wrs.examsystem.util.wheel.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    StudentMapper studentMapper;

    @Resource
    TeacherMapper teacherMapper;
    @Resource
    StudentService studentService;

    public R login(LoginBean loginBean){
        loginBean.setPassword(MD5Util.md5(loginBean.getPassword()));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",loginBean.getUsername());
        wrapper.eq("password",loginBean.getPassword());
        wrapper.eq("user_type",loginBean.getUserType());
        User user = userMapper.selectOne(wrapper);
        if (user == null){
            return R.error().message("用户不存在");
        }else {
            String token = JWTUtil.createToken(user.getUsername());
            return R.ok().message(token);
        }
    }

    /**
     * 需要添加到两张表中
     * @param enrollBean 注册类
     * @return
     */
    @Override
    public R enroll(EnrollBean enrollBean) {
        User user = UserUtil.enrollInvertUser(enrollBean);
//        插入user表
        userMapper.insert(user);
//        插入另外一个表
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
//            查看userID
        User one = userMapper.selectOne(wrapper);
        Student student;
        Teacher teacher;
        int insert;
        if (enrollBean.getUserType() == 2){
            student = UserUtil.enrollInvertStudent(enrollBean);
            student.setUserId(one.getUserId());
            insert = studentMapper.insert(student);
        }else {
            teacher = UserUtil.enrollInvertTeacher(enrollBean);
            teacher.setUserId(one.getUserId());
            insert = teacherMapper.insert(teacher);
        }
        if (insert != 1){
            return R.error().message("注册失败");
        }
        return R.ok().message("注册成功");
    }

    @Override
    public R checkUser(NewUserBean newUserBean) {
        newUserBean.setNewPassword(MD5Util.md5(newUserBean.getNewPassword()));
        newUserBean.setOldPassword(MD5Util.md5(newUserBean.getOldPassword()));
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_number",newUserBean.getStudentNumber());
        String number  = newUserBean.getStudentNumber();
        Student student = studentService.getOne(studentQueryWrapper);
        User user = userMapper.selectById(student.getUserId());
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if(number == null){
            return R.error().message("不存在该用户");
        }
        if(user.getPassword().equals(newUserBean.getOldPassword())
                && student.getStudentNumber().equals(newUserBean.getStudentNumber())){
            userMapper.updateUserPassword(newUserBean.getNewPassword(),newUserBean.getStudentNumber());
        }
        return R.ok();
    }

    @Override
    public R updateByName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User newPwd = (User)wrapper.getEntity().setPassword("CZMM@!68");

        return R.ok().message("管理管重置密码");
    }

    @Override
    public R updateInformation(EnrollBean enrollBean) {

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        String number = enrollBean.getNumber();

        if(enrollBean.getUserType() == 1){
            teacherQueryWrapper.eq("teacher_number",number);
            teacherMapper.updateTeacherInformation(number,enrollBean.getName(), Objects.equals(enrollBean.getSex(), "男") ?0:1);

        }else{
            studentQueryWrapper.eq("student_number",number);
            studentMapper.updateStudentInformation(number, Objects.equals(enrollBean.getSex(), "男") ?0:1,enrollBean.getClasses(),enrollBean.getName());
        }

        userMapper.updateUserInformateion(enrollBean.getNickname(),enrollBean.getNumber());
        return R.ok().message("修改个人信息成功");
    }


}
